function(generate_windows_version_info TARGET)
	# The file is only used on windows
	if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Windows")
		return()
	endif()

	cmake_parse_arguments(
		ARG
		""
		"DESCRIPTION"
		"ICONS"
		${ARGN}
	)

	set(NAME "$<TARGET_FILE_BASE_NAME:${TARGET}>")
	set(FILENAME "$<TARGET_FILE_NAME:${TARGET}>")

	# If no description is supplied, just use the target name
	set(DESCRIPTION "${NAME}")
	if(ARG_DESCRIPTION)
		string(APPEND DESCRIPTION " (${ARG_DESCRIPTION})")
	endif()

	set(INDEX 1)
	foreach(ICON ${ARG_ICONS})
		string(APPEND ICONS "IDI_ICON${INDEX} ICON DISCARDABLE \"${CMAKE_CURRENT_SOURCE_DIR}/${ICON}\"\n")
		math(EXPR INDEX "${INDEX} + 1")
	endforeach()

	# First replace the variables
	configure_file(
		"${CMAKE_SOURCE_DIR}/cmake/templates/version.rc.in"
		"${TARGET}-version.rc.in"
	)

	# Then interpret the generator expressions to get the final version file.
	# The output file is generated for each configuration and the name should be
	# unique, so append it the configuration name.
	set(CONFIG_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_$<CONFIG>.rc")
	file(GENERATE
		OUTPUT "${CONFIG_VERSION_FILE}"
		INPUT "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}-version.rc.in"
	)

	# Now we have a proper resource file which needs to be added to the target
	# sources. But this file is generated at the end of the configuration process,
	# so cmake will error out because it is unable to find the file.
	# The solution is to mark the file as generated, which should be as simple as
	# setting the property to it. Unfortunately the functions for setting source
	# file properties do not support generator expressions yet in the file name,
	# so we cannot use them.
	# A workaroud is to use a custom command to copy the file, which will cause
	# it to be marked as generated by cmake.
	set(VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.rc")
	add_custom_command(
		OUTPUT "${VERSION_FILE}"
		COMMENT "Creating version file for ${TARGET}"
		COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${CONFIG_VERSION_FILE}" "${VERSION_FILE}"
		DEPENDS "${CONFIG_VERSION_FILE}"
	)

	# Add the version file to the target sources
	target_sources("${TARGET}" PRIVATE "${VERSION_FILE}")
endfunction()
